﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ActionSystem.Core;
namespace ActionSystem.Core.ComboLogic
{
    /// <summary>
    /// 受击处理,包括状态处理，伤害计算
    /// </summary>
    public class BaseComboProcessAbility
    {
        BaseStateMachine _stateMachine;
        public BaseHitMessageProcessor hitProcessor;
        private List<HitMessage> hits=new List<HitMessage>();
        private List<BaseHitResponse> responses = new List<BaseHitResponse>();
        public BaseStateMachine StateMachine => _stateMachine;
        public BaseComboProcessAbility(BaseStateMachine baseStateMachine)
        {
            _stateMachine = baseStateMachine;
        }
        /// <summary>
        /// 设置碰撞盒
        /// </summary>
        public virtual void OnHitBoxSetUp()
        {
            //执行当前状态的逻辑
            var currentState = _stateMachine.CurrentState;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IHitBoxActionHandler)?.BoxSetUp(action);
            }

            //判断是否需要执行下一个状态
            var nextState = _stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IHitBoxActionHandler)?.BoxSetUp(action);
            }
        }
        /// <summary>
        /// 执行交互检测算法，产生交互信息
        /// </summary>
        public virtual void OnHitDetect()
        {
            //执行当前状态的逻辑
            var currentState = _stateMachine.CurrentState;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IHitBoxActionHandler)?.HitDetect(action);
            }

            //判断是否需要执行下一个状态
            var nextState = _stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }

            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IHitBoxActionHandler)?.HitDetect(action);
            }
        }
        /// <summary>
        /// 处理交互信息（ComboMessage），
        /// </summary>
        public void HitMessageProccess()
        {
            //处理攻击信息
            foreach(var h in hits)
            {
                var res = hitProcessor?.HandleHitMessage(h);
                if(res!=null)
                {
                    if(h.other!=null)
                    {
                        (h.other as IHitProcessAbility)?.GetHitProcessAbility()?.AddHitResponseMessage(res.hittedResponse);
                    }
                    AddHitResponseMessage(res.attackerResponse);
                    res.PoolItemDelete();
                }else
                {
                    //使用全局处理
                    var r = ActionSystemManager.Instance.HitMessageProcessor?.HandleHitMessage(h);
                    if (r != null)
                    {
                        if (h.other != null)
                        {
                            (h.other as IHitProcessAbility)?.GetHitProcessAbility()?.AddHitResponseMessage(r.hittedResponse);
                        }
                        AddHitResponseMessage(r.attackerResponse);
                        r.PoolItemDelete();
                    }
                }
            }
            //放回池中
            foreach(var hit in hits)
            {
                hit.PoolItemDelete();
            }
            hits.Clear();
        }
        //响应受击信息
        public void ResponseHit()
        {
            //排序
            responses.Sort((a, b) => b.Priority - a.Priority);
            foreach(var r in responses)
            {
                //是否阻止其他信息
                if(r.Handle())
                {
                    break;
                }
            }
            //删除
            foreach (var r in responses)
            {
                r.PoolItemDelete();
            }
            responses.Clear();
        }
        /// <summary>
        /// 添加交互信息
        /// </summary>
        /// <param name="hit"></param>
        public void AddHitMessage(HitMessage hit)
        {
            if(!hits.Contains(hit))
            {
                hits.Add(hit);
            }
        }
        /// <summary>
        /// 添加响应信息
        /// </summary>
        /// <param name="res"></param>
        public void AddHitResponseMessage(BaseHitResponse res)
        {
            if (!responses.Contains(res))
            {
                responses.Add(res );
            }
        }

        public void SetUpHitProcessor(BaseHitMessageProcessor processor)
        {
            this.hitProcessor = processor;
        }
    }
}

